package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.dao.PositionListDao;
import com.coocaa.internship.pojo.PositionList;
import com.coocaa.internship.service.PositionListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :hhd
 * @Description:
 * @date :2019/8/8 9:23
 */

@Service
public class PositionListServiceImpl implements PositionListService {

    @Autowired
    PositionListDao positionListDao;

    private List<PositionList> analysisPositionList(List<PositionList> positionLists) {
        List<PositionList> return_positionLists = new ArrayList<>();
        for (PositionList positionList : positionLists) {
            String positionLabel = positionList.getPositionLabel();
            if (positionLabel != null) {
                String[] positionLabels = positionLabel.split("[,-]");
                List<String> positionLabelList = new ArrayList<>();
                for (String s : positionLabels) {
                    if (s.length() <= 2) {
                        positionLabelList.add(s);
                    }
                    if (positionLabelList.size() >= 2) {
                        break;
                    }
                }
                positionList.setPositionLabelList(positionLabelList);
                positionList.setPositionLabel(null);
            }
            return_positionLists.add(positionList);
        }

        return return_positionLists;
    }

    @Override
    public ResultData selectAllPositionList(Integer pn) {
        PageHelper.startPage(pn, 15);
        List<PositionList> positionLists = positionListDao.selectAllPositionList();
        if (!positionLists.isEmpty()) {
            List<PositionList> return_positionLists = analysisPositionList(positionLists);
            PageInfo<PositionList> pageInfo = new PageInfo<>(return_positionLists, 5);
            return ResultData.success(pageInfo);
        }
        return ResultData.success("没有简历信息");
    }

    @Override
    public ResultData selectPositionListByPositionName(String positionName, Integer pn) {
        String positionName_new = "%" + positionName + "%";
        PageHelper.startPage(pn, 15);
        List<PositionList> positionLists = positionListDao.selectPositionListByPositionName(positionName_new);
        if (!positionLists.isEmpty()) {
            List<PositionList> return_positionLists = analysisPositionList(positionLists);
            PageInfo<PositionList> pageInfo = new PageInfo<>(return_positionLists, 5);
            return ResultData.success(pageInfo);
        }
        return ResultData.success("没有简历信息");
    }

    @Override
    public ResultData selectPositionListByPositionArea(String positionArea, Integer pn) {
        PageHelper.startPage(pn, 15);
        List<PositionList> positionLists = positionListDao.selectPositionListByPositionArea(positionArea);
        if (!positionLists.isEmpty()) {
            List<PositionList> return_positionLists = analysisPositionList(positionLists);
            PageInfo<PositionList> pageInfo = new PageInfo<>(return_positionLists, 5);
            return ResultData.success(pageInfo);
        }
        return ResultData.success("没有简历信息");
    }

    @Override
    public ResultData selectPositionListByPositionNameOrCompany(String positionNameOrCompany, Integer pn) {
        String positionNameOrCompany_new = "%" + positionNameOrCompany + "%";
        PageHelper.startPage(pn, 15);
        List<PositionList> positionLists = positionListDao.selectPositionListByPositionNameOrCompany(positionNameOrCompany_new);
        if (!positionLists.isEmpty()) {
            List<PositionList> return_positionLists = analysisPositionList(positionLists);
            PageInfo<PositionList> pageInfo = new PageInfo<>(return_positionLists, 5);
            return ResultData.success(pageInfo);
        }
        return ResultData.success("没有简历信息");
    }

    @Override
    public ResultData selectPositionListByPositionType(String positionType, Integer pn) {
        PageHelper.startPage(pn, 15);
        List<PositionList> positionLists = positionListDao.selectPositionListByPositionType(positionType);
        if (!positionLists.isEmpty()) {
            List<PositionList> return_positionLists = analysisPositionList(positionLists);
            PageInfo<PositionList> pageInfo = new PageInfo<>(return_positionLists, 5);
            return ResultData.success(pageInfo);
        }
        return ResultData.success("没有简历信息");
    }
}
